from collections import deque

class Worker(object):
    ''':arg
    一个工人根据技能做特定任务

    '''
    time_horizon = None

    def __init__(self, worker_config):
        self.id = worker_config.id
        self.worker_type = worker_config.worker_type
        # self.skill_types = worker_config.skill_types
        # self.duration_map = worker_config.durations
        self.position = worker_config.position
        self._init_position = worker_config.position

        self.board = None # 甲板
        self.task = None #正在执行的task
        self.history = []
        # self.isBusy = False
        # self.machine_door = MachineDoor.NULL
        self.allocated_tasks = deque()

    def reset(self):
        self.position = self._init_position
        self.task = None
        self.allocated_tasks.clear()

    @property
    def isBusy(self):
        if self.task is None:
            return False

        return self.task.started and not self.task.finished

    @property
    def duration(self) ->int:

        # if self.task is None or self.task.finished:
        #     return 0
        duration = 0
        if len(self.allocated_tasks) == 0:
            if self.task is None or self.task.finished:
                return 0
            else:
                duration = self.task.duration
                return duration

        for task in self.allocated_tasks:
            duration += task.duration

        return duration

    def run_task(self):
        # a = []
        # 一个工人一次执行一个任务
        # if not self.isBusy :
            # self.task = self.allocated_tasks.pop()
        self.task.schedule(self)

        # self.task = task
        # self.history.append(task)

        # self.machine_door = MachineDoor.TASK_IN


    @property
    def has_unfinish_tasks(self):
        '''判断worker是否有需要的工作要运行
        进而判断仿真的推进判断标志
        '''
        if self.isBusy:
            return True

        return self.accommodate_task()

    def accommodate_task(self)->bool:
        '''分配task 任务'''
        if len(self.allocated_tasks) > 0:
            new_task = self.allocated_tasks.popleft()
            self.task = new_task
            self.run_task()
            return True

        return False

    def accommodate(self,task)->bool:
        '''分配task 任务'''
        if task.worker_type == self.worker_type:

            if self.duration + task.duration <= Worker.time_horizon:
                self.allocated_tasks.append(task)
                # return True

                if not self.isBusy:
                    new_task = self.allocated_tasks.popleft()
                    self.task = new_task
                    self.run_task()
                    # task.worker = self
                    # return True

                return True

        return False


    @property
    def running_task(self):
        # ls = []

        if self.task.started and not self.task.finished:

           return self.task

        return None

    @property
    def finished_tasks(self):
        ls = []
        for task in self.history:
            if task.finished:
                ls.append(task)
        return ls

    def attach(self, borad):
        self.borad = borad

    # def accommodate(self, task):
    #     return self.cpu >= task.task_config.cpu and \
    #            self.memory >= task.task_config.memory and \
    #            self.disk >= task.task_config.disk
    #
    # @property
    # def feature(self):
    #     return [self.cpu, self.memory, self.disk]
    #
    # @property
    # def capacity(self):
    #     return [self.cpu_capacity, self.memory_capacity, self.disk_capacity]


    def __str__(self) -> str:

        return \
            'id:'+self.id+\
            '\nskills:'+ str(self.skill_types)+\
             '\nisBusy:'+str(self.isBusy)+\
            '\ncurr_position:'+ str(self.position)



    def __eq__(self, other):
        return isinstance(other, Worker) and other.id == self.id